//
//  MyOrderViewController.m
//  EasyToTesco
//
//  Created by mc on 16/2/22.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "MyOrderViewController.h"

#import "HeaderFileForEasyToTesco.h"

#import "MyOrderWholeTableViewCell.h"
#import "FOrderTableViewCell.h"

//选择控制器视图
#import "SelectBtnView.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
<<<<<<< HEAD
#import "ReadLoginData.h"

#import "oilTableViewCell.h"
=======



>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee

//orderDetailVC
#import "OrderDetailViewController.h"


@interface MyOrderViewController ()<UITableViewDataSource, UITableViewDelegate, SelectBtnViewDelegate>
{
    NSInteger orderstate;
    BOOL BtnState;
    NSString *usernameStr;
}


@property (nonatomic, strong)NSMutableArray *WholeArray;
@property (nonatomic, strong)NSMutableArray *fOrderArray;
@property (nonatomic, strong)NSMutableArray *OilorderArray;
<<<<<<< HEAD
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)UITableView *MyOrderTableview;
@property (nonatomic, copy)NSString *userName;
=======
@property (nonatomic, strong)UITableView *MyOrderTableview;

>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
@end

@implementation MyOrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
<<<<<<< HEAD
    [self getdata];
=======
    
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    [self addMyOrderVcinit];
    [self addMyOrderTopView];
    [self addMyOrderUI];
    [self addMyOrdertableview];
    
    [self MyOrderDataSources];
    
}
<<<<<<< HEAD
- (void)getdata {
    // 获取当前账号
    NSDictionary *dic = [ReadLoginData readLoginData];
    if (dic.allKeys.count == 0) {
        UIAlertView *alview = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您尚未登录,请登录后再次操作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alview show];
    }else {
        usernameStr = [dic[@"data"] objectForKey:@"username"];
    }
    
}
- (void)addMyOrderVcinit {
    orderstate = 1000;
    BtnState = YES;
 //   usernameStr = @"13282008515";
=======

- (void)addMyOrderVcinit {
    orderstate = 1000;
    BtnState = YES;
    usernameStr = @"13282008515";
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    self.WholeArray = [@[]mutableCopy];
    self.fOrderArray = [@[]mutableCopy];
    self.OilorderArray = [@[]mutableCopy];
    
}

- (void)addMyOrderTopView {

    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"我的订单";
}

- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)addMyOrdertableview {
    self.MyOrderTableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.16, WEITH, HIGHT*0.84) style:UITableViewStyleGrouped];
    self.MyOrderTableview.dataSource = self;
    self.MyOrderTableview.delegate = self;
    self.MyOrderTableview.tableFooterView = [[UIView alloc] init];
    self.MyOrderTableview.rowHeight = 80;
    [self.view addSubview:self.MyOrderTableview];
}

- (void)addMyOrderUI {
    
    SelectBtnView *selectView =[[SelectBtnView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.06)];

    selectView.delegate = self;

    [self.view addSubview:selectView];
    
}

#pragma mark -- SelectBtnViewDelegate
-(void)selectBtnClick:(UIButton *)button {
<<<<<<< HEAD
//    NSLog(@"%@----%ld", button.titleLabel.text, button.tag);
=======
    NSLog(@"%@----%ld", button.titleLabel.text, button.tag);
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
   
    orderstate = button.tag;
    
    if (button.tag == 1000) {
        BtnState = YES;
        [self MyOrderDataSources];
    } else if (button.tag == 1001) {
        BtnState = NO;
        [self MyOrderGethfordDataSources];
       
    } else {
        BtnState = NO;
        [self MyOrderGethoilDataSources];
    }
    
    
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (BtnState == YES) {
        static NSString *identifier = @"MyOrderWholeTableViewCell";
        MyOrderWholeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"MyOrderWholeTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        
        NSString *tempStr = [[[NSString stringWithFormat:@"%@", [[self.WholeArray[indexPath.row] objectForKey:@"picprex"] objectForKey:@"m"]] componentsSeparatedByString:@" "] firstObject];
        
        
        NSString *imageStr = [NSString stringWithFormat:@"%@%@", tempStr, [self.WholeArray[indexPath.row] objectForKey:@"photos"]];
        
        
        
        [cell.wholePic sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@"cellpic.png"]];
        cell.wholeIntrolabel.text = [NSString stringWithFormat:@"%@", [self.WholeArray[indexPath.row] objectForKey:@"intro"]];
        
        cell.wholeTimelabel.text = [NSString stringWithFormat:@"下单时间:%@", [self.WholeArray[indexPath.row] objectForKey:@"addtime"]];
        
        cell.wholeValuelabel.text = [NSString stringWithFormat:@"价格:%@", [self.WholeArray[indexPath.row] objectForKey:@"zongjia"]];
        cell.wholeStatelabel.text = [NSString stringWithFormat:@"订单状态:%@", [self.WholeArray[indexPath.row] objectForKey:@"zt"]];
        
        cell.wholeNumLabel.text = [NSString stringWithFormat:@"数量:%@", [self.WholeArray[indexPath.row] objectForKey:@"num"]];
        return cell;
    } else {
        static NSString *identifier = @"FOrderTableViewCell";
        FOrderTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"FOrderTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            
        }
        
        if (orderstate == 1001) {
<<<<<<< HEAD
            
=======
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
            cell.ForderCountLabel.text = [NSString stringWithFormat:@"手机号码:%@", [self.fOrderArray[indexPath.row] objectForKey:@"account"]];
            cell.ForderJineLabel.text = [NSString stringWithFormat:@"充值金额:%@", [self.fOrderArray[indexPath.row] objectForKey:@"jine"]];
            cell.Forderfkfslabel.text = [NSString stringWithFormat:@"付款方式:%@", [self.fOrderArray[indexPath.row] objectForKey:@"fkfs"]];
            cell.ForderZtlabel.text = [NSString stringWithFormat:@"充值状态:%@", [self.fOrderArray[indexPath.row] objectForKey:@"zt"]];
            cell.ForderTimeLabel.text = [NSString stringWithFormat:@"充值时间:%@", [self.fOrderArray[indexPath.row] objectForKey:@"addtime"]];
<<<<<<< HEAD
            
            if ([[self.fOrderArray[indexPath.row] objectForKey:@"zt"] isEqualToString:@"1"]) {
                
                 cell.ForderZtlabel.text = [NSString stringWithFormat:@"充值状态:%@", @"成功"];
                
            }else{
                cell.ForderZtlabel.text = [NSString stringWithFormat:@"充值状态:%@", @"失败"];

                
            }
            
        }
     
=======
        }
        
        
        
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        return cell;
    }
   
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (orderstate == 1000) {
        return self.WholeArray.count;
    } else if (orderstate == 1001){
        return self.fOrderArray.count;
    } else if (orderstate == 1002){
        return self.OilorderArray.count;
    }else {
        return self.WholeArray.count;
    }
}

#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
        return 1;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (BtnState == NO) {
        return 0.1;
    } else {
    return 30;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *OrderFootview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WEITH, 30)];
    OrderFootview.backgroundColor = [UIColor whiteColor];
    if (BtnState == YES) {
        [self footViewaddUI:OrderFootview];
    }
    
    return OrderFootview;
}

//在footview上添加UI控件
- (void)footViewaddUI:(UIView *)subview {
    UIImageView *plusImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 5, CGRectGetHeight(subview.frame)-10, CGRectGetHeight(subview.frame)-10)];
    plusImageView.image = [UIImage imageNamed:@"减免.png"];
    [subview addSubview:plusImageView];
    
    UILabel *titlebel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(plusImageView.frame)+5, 5, 100, CGRectGetHeight(subview.frame)-10)];
    titlebel.text = @"满100元减400元";
    titlebel.font = [UIFont systemFontOfSize:12];
    [subview addSubview:titlebel];
    
    NSArray *titleArray = @[@"收货码", @"派送中", @"未付款"];
    CGFloat weiht = 50;
    for (NSInteger i=0; i<3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(WEITH - weiht*3-5*3 +(weiht+5)*i, 5, weiht, CGRectGetHeight(subview.frame)-10);
        button.tag = 330+i;
        button.layer.borderWidth = 0.5;
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(footBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [subview addSubview:button];
    }
}

- (void)footBtnClick:(UIButton *)button {
    NSLog(@"%ld", button.tag);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld,,,%ld", indexPath.section, indexPath.row);
    
    OrderDetailViewController *orderDetailVC = [[OrderDetailViewController alloc] init];
    if (orderstate == 1000) {
        orderDetailVC.orderDetailId = [NSString stringWithFormat:@"%@", [self.WholeArray[indexPath.row] objectForKey:@"ordid"]];
    }
    [self presentViewController:orderDetailVC animated:YES completion:nil];
    
}


/**
 *  给UITextField设置右侧的图片
 *
 *  @param textField UITextField
 *  @param imageName 图片名称
 */

-(void)setLeftViewWithTextField:(UITextField *)textField imageName:(NSString *)imageName{
    
    UIImageView *rightView = [[UIImageView alloc]init];
    
    rightView.image = [UIImage imageNamed:imageName];
    rightView.frame = CGRectMake(0, 0, CGRectGetHeight(textField.frame), CGRectGetHeight(textField.frame));
    rightView.contentMode = UIViewContentModeCenter;
    textField.leftView = rightView;
    textField.leftViewMode = UITextFieldViewModeAlways;
}

//wholeorder

- (void)MyOrderDataSources {
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = usernameStr;
    NSString *zt = @"0";

    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username], @"zt":[NSString stringWithFormat:@"%@", zt]};
    
    [mgr POST:@"http://www.ylgw8.com/AppOrd/Getord" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        self.WholeArray = [self toArrayOrNSDictionary:responseObject];
        
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:self.WholeArray[0]];
        
<<<<<<< HEAD
//        
//        NSLog(@"%@", self.WholeArray);
//        
//        NSLog(@"%@", dic);
//        
//        NSLog(@"d订单%@", string);
//    
//        
=======
        
        NSLog(@"%@", self.WholeArray);
        
        NSLog(@"%@", dic);
        
        NSLog(@"d订单%@", string);
    
        
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        [self.MyOrderTableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

<<<<<<< HEAD






=======
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
//forder
- (void)MyOrderGethfordDataSources {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
<<<<<<< HEAD
   // NSString *username = @"18620614920";//usernameStr;
    NSString *username = usernameStr;//usernameStr;
=======
    NSString *username = @"18620614920";//usernameStr;
    
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username]};
    
    [mgr POST:@"http://www.ylgw8.com/AppOrd/Gethford" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        self.fOrderArray = [self toArrayOrNSDictionary:responseObject];
        
//        NSLog(@"ford--%@", string);
        
        
        [self.MyOrderTableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
         //         NSLog(@"数据请求错误原因:%@", error);
         //         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

//oilorder
- (void)MyOrderGethoilDataSources {
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *username = usernameStr;
    
    NSDictionary *param = @{@"username":[NSString stringWithFormat:@"%@", username]};
    
    [mgr POST:@"http://www.ylgw8.com/AppOrd/Getykord" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
<<<<<<< HEAD
        
=======
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        NSLog(@"Oilord--%@", string);
        
<<<<<<< HEAD
        //  石油订单
        self.OilorderArray = [self toArrayOrNSDictionary:responseObject];
        
        
=======
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
        
        [self.MyOrderTableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
                  NSLog(@"error:%@", error);
                  NSLog(@"errorcode:%ld", (long)operation.response.statusCode);
     }];
}


// 将JSON串转化为字典或者数组
- (id)toArrayOrNSDictionary:(NSData *)jsonData{
    NSError *error = nil;
    id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData
                                                    options:NSJSONReadingAllowFragments
                                                      error:&error];
    
    if (jsonObject != nil && error == nil){
        return jsonObject;
    }else{
        // 解析错误
        return nil;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
